package com.example.activity.controller;

import com.example.activity.entity.Activity;
import com.example.activity.service.ActivityService;
import com.example.adminuser.dto.ActivityPage;
import com.example.adminuser.dto.GoodsPage;
import com.example.goods.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 14:05 2019/4/4
 */
@Controller
public class ActivityController {
    @Autowired
    @Qualifier("activityServiceImpl")
    private ActivityService activityService;

    @RequestMapping("/activitylist")
    public String activity(Model model,@RequestParam(value = "offset",required = false) Integer offset){
        if (offset==null){
            offset=0;
        }
        List<Activity> activityList=activityService.selectActivityPaginationList(offset);
        int count =activityService.activityListCount();
        int currentPage;
        if (offset==0){
            currentPage=1;
        }else {

            currentPage=offset;
        }
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("allPage",count);
         model.addAttribute("activityList",activityList);
        return "activitylist";
    }
    @RequestMapping("/activityDetail/{activityId}")
    public String activityDetail(@PathVariable("activityId") Long activityId, Model model){
        Activity activity=activityService.selectActivityDetail(activityId);
        model.addAttribute("activity",activity);
        return "activityDetail";
    }
    @GetMapping("/activityList")
    @ResponseBody
    public ActivityPage activityPageList(int offset, int limit){
        ActivityPage activityPage=new ActivityPage();
        List<Activity> activityList=activityService.selectActivityListPage(offset,limit);
        int count=activityService.selectActivityPageCount();
        activityPage.setTotal(count);
        activityPage.setRows(activityList);
        return activityPage;
    }
    @RequestMapping("/admin/activityadd")
    @ResponseBody
    public String addActivity(@RequestBody Activity activity){
        activityService.addActivity(activity);
      return "succ";
    }
    @RequestMapping("/admin/publishactivity")
    @ResponseBody
    public String publishActivity(@RequestBody Activity activity){
        activityService.publishActivity(activity);
        return "succ";
    }
    @RequestMapping("/admin/deleteactivity")
    @ResponseBody
    public String deleteActivity(@RequestBody Activity activity){
        activityService.deleteActivity(activity);
        return "succ";
    }
}
